import { defHttp } from '@/utils/http/axios'
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'
import { ErrorMessageMode } from '#/axios'

enum Api {
  GetUserList = '/user/pageList',
  AddOrUpdateUser = '/user/insertOrUpdate',
  StopOrStartUser = '/user/stopOrStart',
  DivideRole = '/user/divideRole',
  ResetPassword = '/user/resetPassword',
  DeleteUser = '/user/delete',
  ChangePassword = '/user/changePassword',
  Login = '/login',
  Logout = '/logout',
  GetUserInfo = '/getUserInfo',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
  MonthLossAdd = '/monthLoss/add',
}

export const postUserList = (data?: any) => defHttp.post({ url: Api.GetUserList, data })
export const addOrUpdateUser = (data?: any) => defHttp.post({ url: Api.AddOrUpdateUser, data })
export const addMonthLossAdd = (data?: any) => defHttp.post({ url: Api.MonthLossAdd, data })
export const stoporstartUser = (data?: any) => defHttp.post({ url: Api.StopOrStartUser, data })
export const divideRole = (data?: any) => defHttp.post({ url: Api.DivideRole, data })
export const resetPassword = (userId: string | string[]) =>
  defHttp.get({ url: Api.ResetPassword, params: { userId } })
export const deleteUser = (userId: string) =>
  defHttp.get({ url: Api.DeleteUser, params: { userId } })
export const changePassword = (data?: any) => defHttp.post({ url: Api.ChangePassword, data })

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  )
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' })
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode })
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout })
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  )
}
